package xyz.hubery.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import xyz.hubery.domain.SysUser;
import xyz.hubery.domain.WorkIssue;
import xyz.hubery.model.R;
import xyz.hubery.service.SysUserService;
import xyz.hubery.service.WorkIssueService;

@RestController
@Api(tags = "问题工单")
@RequestMapping("/workIssues")
public class WorkIssueController {

    @Autowired
    private WorkIssueService workIssueService;
    @Autowired
    private SysUserService userService;


    @GetMapping
    @ApiOperation(value = "分页条件查询工单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "工单当前的处理状态"),
            @ApiImplicitParam(name = "startTime", value = "工单创建的起始时间"),
            @ApiImplicitParam(name = "endTime", value = "工单创建的截至时间"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数"),
    })
    @PreAuthorize("hasAuthority('work_issue_query')")
    public R<Page<WorkIssue>> findByPage(@ApiIgnore Page<WorkIssue> page,
                                         Integer status, String startTime, String endTime){
        Page<WorkIssue> workIssuePage = workIssueService.findByPage(page,status,startTime,endTime);
        return R.ok(workIssuePage);
    }


    @PatchMapping
    @ApiOperation(value = "回复某个工单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "工单的 ID"),
            @ApiImplicitParam(name = "answer", value = "工单的 answer"),
    })
    @PreAuthorize("hasAuthority('work_issue_update')")
    public R work_issue_update(Long id ,String answer) {
        WorkIssue workIssue = new WorkIssue();
        workIssue.setStatus(2);
        workIssue.setId(id);
        workIssue.setAnswer(answer);
        // 设置回复人信息
        String userIdStr = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        workIssue.setAnswerUserId(Long.valueOf(userIdStr));
        // 根据id查询用户名称
        SysUser sysUser = userService.getById(Long.valueOf(userIdStr));
        workIssue.setAnswerName(sysUser.getUsername());
        boolean b = workIssueService.updateById(workIssue);
        if(b){
            return R.ok();
        }
        return R.fail();
    }

    @PostMapping("/addWorkIssue")
    @ApiOperation("前台用户发起工单")
    public R addWorkIssue(@RequestBody @Validated WorkIssue workIssue) {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        workIssue.setUserId(userId);
        workIssue.setStatus(1);
        boolean save = workIssueService.save(workIssue);
        return save ? R.ok() : R.fail("工单提交失败");
    }

    @GetMapping("/issueList")
    @ApiOperation(value = "前台用户分页查询自己的工单")
    public R<Page<WorkIssue>> issueList(@ApiIgnore Page<WorkIssue> page ) {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        Page<WorkIssue> pageData = workIssueService.issueList(userId,page);
        return R.ok(pageData);
    }



}
